package pers.cz.codegen.dbtools;

import pers.cz.codegen.enums.DbConfigItem;
import pers.cz.jefconfig.config.JefConfiguration;
import pers.cz.jefconfig.tools.StringUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

/**
 * @program: LuoThinking-cloud
 * @description: 抽象数据库相关操作类
 * @author: Cheng Zhi
 * @create: 2022-08-08 10:59
 **/
public abstract class DataBaseOperator {

    Connection connection;

    Statement statement;

    public DataBaseOperator() {

        String dbType = JefConfiguration.get(DbConfigItem.DB_TYPE);
        String driver = "";
        String url = JefConfiguration.get(DbConfigItem.DB_URL);
        String userName = JefConfiguration.get(DbConfigItem.DB_USERNAME);
        String passWord = JefConfiguration.get(DbConfigItem.DB_PASSWORD);
        if (dbType == null || "".equals(dbType)) {
            dbType = StringUtils.split(url, ":")[1];
        }

        if (dbType.equalsIgnoreCase("mysql")) {
            driver = "com.mysql.jdbc.Driver";
        } else if (dbType.equalsIgnoreCase("oracle")) {
            driver = "oracle.jdbc.driver.OracleDriver";
        } else if (dbType.equalsIgnoreCase("mariadb")) {
            driver = "org.mariadb.jdbc.Driver";
        } else if (dbType.equalsIgnoreCase("postgresql")) {
            driver = "org.postgresql.Driver";
        }
        getSatement(driver, url, userName, passWord);

    }

    /**
     * 获取statement对象
     * @param driver
     * @param url
     * @param userName
     * @param passWord
     * @return
     */
    public Statement getSatement(String driver, String url, String userName, String passWord) {

        try {
            Properties props =new Properties();

            Class.forName(driver);

            props.setProperty("user", userName);

            props.setProperty("password", passWord);

            props.setProperty("remarks", "true"); //设置可以获取remarks信息

            props.setProperty("useInformationSchema", "true");//设置可以获取tables remarks信息
            // 通过反射获取驱动
            Object o = Class.forName(driver).newInstance();
            // 保证单例
            if (connection == null) {
                //connection = DriverManager.getConnection(url, userName, passWord);
                connection = DriverManager.getConnection(url, props);
            }

            if (statement == null) {
                statement = connection.createStatement();
            }
            return statement;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("连接数据库出错");
        }

        return statement;
    }

    /**
     * 获取Connection对象
     * @param driver
     * @param url
     * @param userName
     * @param passWord
     * @return
     */
    public Connection getConnection(String driver, String url, String userName, String passWord) {

        try {
            // 通过反射获取驱动
            Object o = Class.forName(driver).newInstance();
            // 保证单例
            if (connection == null) {
                connection = DriverManager.getConnection(url, userName, passWord);
            }
            return connection;
        } catch (Exception e) {
            System.out.println("连接数据库出错");
        }

        return connection;
    }
}
